package com.shm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.ProductLike;
import com.ruoyi.common.core.domain.entity.Star;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.shm.service.IProductLikeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品——点赞Controller
 *
 * @author dam
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/market/like")
public class ProductLikeController extends BaseController {
    @Autowired
    private IProductLikeService productLikeService;

    /**
     * 查询商品——点赞列表
     */
    @PreAuthorize("@ss.hasPermi('market:like:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductLike productLike) {
        startPage();
        List<ProductLike> list = productLikeService.list(new QueryWrapper<ProductLike>(productLike));
        return getDataTable(list);
    }

    /**
     * 导出商品——点赞列表
     */
    @PreAuthorize("@ss.hasPermi('market:like:export')")
    @Log(title = "商品——点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductLike productLike) {
        List<ProductLike> list = productLikeService.list(new QueryWrapper<ProductLike>(productLike));
        ExcelUtil<ProductLike> util = new ExcelUtil<ProductLike>(ProductLike.class);
        util.exportExcel(response, list, "商品——点赞数据");
    }

    /**
     * 获取商品——点赞详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:like:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(productLikeService.getById(id));
    }

    /**
     * 新增商品——点赞
     */
    @PreAuthorize("@ss.hasPermi('market:like:add')")
    @Log(title = "商品——点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductLike productLike) {
        return toAjax(productLikeService.save(productLike));
    }

    /**
     * 修改商品——点赞
     */
    @PreAuthorize("@ss.hasPermi('market:like:edit')")
    @Log(title = "商品——点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductLike productLike) {
        return toAjax(productLikeService.updateById(productLike));
    }

    /**
     * 删除商品——点赞
     */
    @PreAuthorize("@ss.hasPermi('market:like:remove')")
    @Log(title = "商品——点赞", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(productLikeService.removeByIds(ids));
    }

    /**
     * 点赞商品
     */
    @PreAuthorize("@ss.hasPermi('shm:star:list')")
    @GetMapping("/starProduct/{productId}")
    public AjaxResult like(@PathVariable("productId") Long productId) {
        ProductLike productLike = new ProductLike();
        productLike.setUserId(getLoginUser().getUserId());
        productLike.setProductId(productId);
        boolean save = productLikeService.save(productLike);
        return AjaxResult.success();
    }
}
